﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using ComLib.ConfigService;
using ComLib.ComType;
using ComLib;
using System.Reflection;
using System.IO;
using ComLib.ComFun;

namespace EECMISWS
{
    [XmlRootAttribute("WSClientOption", Namespace = "EECMISWS", IsNullable = false)]
    public class WSClientOption : XmlSerializeBase
    {
        [XmlIgnore]
        public override string ConfigDirectory
        {
            get
            {
                string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
                directory = directory.Substring(6);
                return directory;
            }
        }

        /// <summary>
        /// 是否上传
        /// </summary>
        private bool _enabled = false;
        [XmlElementAttribute("Enabled")]
        public bool Enabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }

        private bool _uploadSXQM = false;
        [XmlElementAttribute("UploadSXQM")]
        public bool UploadSXQM
        {
            get { return _uploadSXQM; }
            set { _uploadSXQM = value; }
        }

        /// <summary>
        /// 系统平台名称
        /// </summary>
        private string _xtmc = "系统平台";
        [XmlElementAttribute("xtmc", IsNullable = true)]
        public string XTMC
        {
            get { return _xtmc; }
            set { _xtmc = value; }
        }

        /// <summary>
        /// 用户名
        /// </summary>
        private string _user;
        [XmlElementAttribute("user", IsNullable = true)]
        public string User
        {
            get { return _user; }
            set { _user = value; }
        }

        /// <summary>
        /// 登录密码
        /// </summary>
        private string _ciphertext;
        [XmlElementAttribute("pwd", IsNullable = true)]
        public string Ciphertext
        {
            get { return _ciphertext; }
            set { _ciphertext = value; }
        }

        private string _province = "湖北";
        [XmlElementAttribute("province", IsNullable = true)]
        public string Province
        {
            get { return _province; }
            set { _province = value; }
        }

        private string _url;
        [XmlElementAttribute("url", IsNullable = true)]
        public string URL
        {
            get { return _url; }
            set { _url = value; }
        }

        private string _ywbh_prefix;
        [XmlElementAttribute("ywbh_prefix", IsNullable = true)]
        public string Ywbh_Prefix
        {
            get { return _ywbh_prefix; }
            set { _ywbh_prefix = value; }
        }

        /// <summary>
        /// 形成密文，用于保存
        /// </summary>
        public void MakeCiphertext(string text)
        {
            if (!string.IsNullOrEmpty(text))
            {
                _ciphertext = CustomEncrypt.Encrypt(text);
            }
        }

        /// <summary>
        /// 获得明文
        /// </summary>
        public string GetPassword()
        {
            string password = "";
            if (!string.IsNullOrEmpty(_ciphertext))
            {
                password = CustomEncrypt.Deciphering(_ciphertext);
            }
            return password;
        }

        public override bool Serialize()
        {
            return Serializer.SaveToXml(ConfigFilePath, this);
        }

        public override bool Load()
        {
            WSClientOption obj = (WSClientOption)Serializer.LoadFromXml(ConfigFilePath, typeof(WSClientOption));
            if (obj != null)
            {
                this._xtmc = obj._xtmc;
                this._user = obj._user;
                this._ciphertext = obj._ciphertext;
                this._province = obj._province;
                this._url = obj._url;
                this._enabled = obj._enabled;
                this._nVer = obj._nVer;
                this._uploadSXQM = obj._uploadSXQM;   
                this._ywbh_prefix = obj._ywbh_prefix;
                return true;
            }
            else
            {
                ErrorMessage = Serializer.Message;
                return false;
            }
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("Aisino全国照片上传接口账户选项: ");
            Action<string, object> FillString =
                (string field, object val) =>
                {
                    if (val != null)
                    {
                        builder.Append("[");
                        builder.Append(field);
                        builder.Append("]: ");
                        builder.Append(val.ToString() + "; ");
                    }                 
                };
            FillString("是否启用", _enabled ? "是" : "否");
            FillString("系统平台", _xtmc);
            FillString("省份", _province);
            FillString("用户名", _user);
            FillString("密码", GetPassword());
            FillString("URL", _url);
            FillString("业务编号前缀", _ywbh_prefix);
            FillString("是否上传手写签名", _uploadSXQM ? "是" : "否");
            return builder.ToString();
        }
    }
}
